<template>
	<div class="system-dept-container">
		<el-card shadow="hover">
			<div class="system-dept-search mb15">
				<el-form :inline="true">
					<el-form-item>
						<el-button v-if="permissionStore.isPermissionsBtn('001004001')" size="default" type="success" class="ml10" @click="onOpenAddDept">
							<el-icon>
								<FolderAdd />
							</el-icon>
							新增部门
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%" row-key="id" default-expand-all
				>
				<el-table-column prop="label" label="部门名称" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="disabled" label="部门状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.disabled">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope" v-if="permissionStore.isPermissionsBtn(['001004002','001004003','001004001'])">
						<el-button v-if="permissionStore.isPermissionsBtn('001004001')" size="small" text type="primary" @click="onOpenAddDept(scope.row)">新增</el-button>
						<el-button v-if="permissionStore.isPermissionsBtn('001004002')" size="small" text type="warning" @click="onOpenEditDept(scope.row)">修改</el-button>
						<el-button v-if="permissionStore.isPermissionsBtn('001004003')" size="small" text type="danger" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<EditDept ref="editDeptRef" @deptList="deptList"  :acType="acType"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, defineComponent, getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EditDept from './component/editDept.vue';
import { treeDeptApi,delDeptApi } from "@/api/auth/dept";
import {usePermissionStore} from '@/store/modules/permission'

// 定义接口来定义对象的类型
interface TableDataRow {
	id: number;
	label:String,
	disabled:Boolean,
}
interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		loading: boolean;
	};
}

export default defineComponent({
	name: 'systemDept',
	components: { EditDept },
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const editDeptRef = ref();
		  const permissionStore = usePermissionStore()
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				loading: false,
			},
		});
		const acType=ref('add')
		// 初始化表格数据
		const initTableData = () => {
			deptList();
		};
		const deptList = () => {
			treeDeptApi().then((res: any) => {
				state.tableData.data = res.data
			});
		};
		// 打开新增菜单弹窗
		const onOpenAddDept = (row?: TableDataRow) => {
			acType.value='add'
			editDeptRef.value.openDialog(row?.id);
		};
		// 打开编辑菜单弹窗
		const onOpenEditDept = (row: TableDataRow) => {
			acType.value='edit'
			editDeptRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: TableDataRow) => {
			ElMessageBox.confirm(`此操作将永久删除部门：${row.label}, 是否继续?`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delDeptApi({id:row.id}).then(() => {
						ElMessage.success('删除成功');
						deptList();
					})
				})
				.catch(() => { });
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			editDeptRef,
			acType,
			permissionStore,
			deptList,
			onOpenAddDept,
			onOpenEditDept,
			onTabelRowDel,
			...toRefs(state),
		};
	},
});
</script>
